﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QuickTasks.GeocodeService;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls;
using LocalSettings;
using System.IO.IsolatedStorage;

namespace QuickTasks.Views.BackGrounds
{

    public static  class Geo 
    {

        public static Distance d = new Distance();
      public static   GeoCoordinateWatcher watcher = null;
       public static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        public static void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => MyStatusChanged(e));
        }

        public static void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
        {
            try
            {

                if (watcher.Permission == GeoPositionPermission.Denied)
                {

                    IsolatedSettings.saveSettings("Locationserviceon", "false");
                }
                else
                {
                    IsolatedSettings.saveSettings("Locationserviceon", "true");
                }

            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in MyStatusChanged method In Geo file", ex); 
            }
        }

        public static void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
        }

        public static void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            try
            {
                ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();
                reverseGeocodeRequest.Credentials = new QuickTasks.GeocodeService.Credentials();
                reverseGeocodeRequest.Credentials.ApplicationId = "AmivBwP5dxW9Uih0JzRpHA_Sln8fHpHw7WB5TOkK9RCXOECJWZAXYU1U8UQ47fUr";
                GeocodeService.UserLocation point = new GeocodeService.UserLocation();
                point.Latitude = Convert.ToDouble(e.Position.Location.Latitude.ToString("0.0000"));
                point.Longitude = Convert.ToDouble(e.Position.Location.Longitude.ToString("0.0000"));
                App.Latitude = point.Latitude;
                App.Longitude = point.Longitude;
                IsolatedSettings.saveSettings("lat", point.Latitude.ToString());
                IsolatedSettings.saveSettings("long", point.Longitude.ToString());
                reverseGeocodeRequest.Location = point;
                GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
                geocodeService.ReverseGeocodeCompleted += new EventHandler<ReverseGeocodeCompletedEventArgs>(geocodeService_ReverseGeocodeCompleted);
                geocodeService.ReverseGeocodeAsync(reverseGeocodeRequest);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in MyPositionChanged method In Geo file", ex); 
            }
        }

        public static void geocodeService_ReverseGeocodeCompleted(object sender, ReverseGeocodeCompletedEventArgs e)
        {
            try
            {
                GeocodeResponse geocodeResponse = e.Result;
                if (geocodeResponse.Results.Count > 0)
                {
                    App.Address = geocodeResponse.Results[0].Address.AddressLine.ToString();
                    App.Location = geocodeResponse.Results[0].Address.Locality.ToString();
                    App.State = geocodeResponse.Results[0].Address.AdminDistrict.ToString();
                    App.ZipCode = geocodeResponse.Results[0].Address.PostalCode.ToString();
                    App.Country = geocodeResponse.Results[0].Address.CountryRegion.ToString();

                }
                d.Cal();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in geocodeService_ReverseGeocodeCompleted event In Geo file", ex); 
            }
        }
       
        public static void whereamI()
        {
            try
            {
                if (watcher == null)
                {
                    string rang = IsolatedSettings.getSettingsStringValue("range");
                    watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                    if (rang == "")
                    {
                        watcher.MovementThreshold = 20;
                    }
                    else
                    {
                        watcher.MovementThreshold =Convert.ToDouble(rang);
                    }
                    watcher.StatusChanged+=new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
                    watcher.PositionChanged+=new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
                }
                watcher.Start();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in whereamI method In Geo file", ex); 
            }
        }
       public static void Stop()
        {
            try
            {
                watcher.Stop();
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in Stop method In Geo file", ex); 
            }
        }
    }
}
